﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Forms.ReadingAmmeter;
using PDAMain.Interface;


namespace PDAMain.Forms.ReadingAmmeter
{
    public partial class AmmeterReadingList : BaseForm
    {
        private string strBarCode = "";
        public AmmeterReadingList()
        {
            InitializeComponent();
        }
        private void BindData()
        {
            DataSet ds = DataHelper.ReadingAmmeterDao.list(); 
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dtmapping = ds.Tables[0];

                dgmeterList.DataSource = dtmapping;
                DataGridTableStyle dts = new DataGridTableStyle();
                dts.MappingName = dtmapping.TableName;

                DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
                col1.HeaderText = "资产条码";
                col1.MappingName = "BARCODE";
                col1.Width = 185;
                dts.GridColumnStyles.Add(col1);

                DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
                col2.HeaderText = "抄表月份";
                col2.MappingName = "READMONTH";
                col2.Width = 64;
                dts.GridColumnStyles.Add(col2); 

                DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
                col3.HeaderText = "上月读数";
                col3.MappingName = "PRE_DEGREE";
                col3.Width = 65;
                dts.GridColumnStyles.Add(col3);

                dgmeterList.TableStyles.Clear();
                dgmeterList.TableStyles.Add(dts);
                dgmeterList.Refresh();
            }
        }
        /// <summary>
        /// 跳转详情页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void dgmeterList_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dgmeterList.DataSource;
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[dgmeterList.CurrentRowIndex]["READDEGREE"].ToString() == "0")
                {
                    string SelectNum = dt.Rows[dgmeterList.CurrentRowIndex]["EM_METER_ID"].ToString();
                    string selectedMprid = dt.Rows[dgmeterList.CurrentRowIndex]["MPRId"].ToString();
                    DataHelper.ReadingAmmeterDao.Am_Meter_Id = SelectNum;
                    DataHelper.ReadingAmmeterDao.MprId = selectedMprid;
                }
            }
        }
        private void AmmeterReadingList_Load(object sender, EventArgs e)
        {
            try
            {
                // 打开BarCode扫描设备
                if (!OpenBarCode())
                {
                    MessageBox.Show("打开扫描接口失败，请重登系统");
                }

                EnableBeeper(true);
                EnableVibrater(true);
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // 重载 基类BaseForm的这个方法
        public override void OnBarCodeNotify(byte[] BarCodeData, int nLength)
        {
            EnableBarCode(false);

            string BarCode = Encoding.Default.GetString(BarCodeData, 0, nLength);
            strBarCode = BarCode;
            EnableBarCode(true);
            bool Flag = MearchData(strBarCode);
            if (Flag)
            {
                AmmeterRecordDetail ARD = new AmmeterRecordDetail();
                ARD.Show();
                this.Close();
                CloseBarCode();
            }
        }

        private void WaterReadingList_Closed(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
            CloseBarCode();
        }
        private bool MearchData(string strBC)
        {
            bool Flag = false;
            DataTable dt = (DataTable)dgmeterList.DataSource;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["BARCODE"].ToString() == strBarCode && dt.Rows[i]["READDEGREE"].ToString() == "0")
                    {
                        DataHelper.ReadingAmmeterDao.Am_Meter_Id = strBarCode;
                        DataHelper.ReadingAmmeterDao.MprId= dt.Rows[i]["MPRId"].ToString();
                        return true;
                    }
                }
            }
            return Flag;
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            AmPlanList fun = new AmPlanList();
            fun.Show();
            this.Close();
        }


      /*  private void menuItem2_Click(object sender, EventArgs e)
        {
            AmPlanList fun = new AmPlanList();
            fun.Show();
            this.Close();
        }*/
    }
}